package com.polaris.live.dialog

import android.content.Context
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewpager2.widget.ViewPager2
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.R
import com.polaris.live.adapter.TogetherDailyAdapter
import com.polaris.live.adapter.TogetherReceiveAdapter
import com.polaris.live.common.util.AppCodeUtils
import com.polaris.live.databinding.DialogTogetherReceiveBinding
import com.polaris.live.resp.back_resp.UserConsumableDailyRewardResp

/**
 * 聚合领取弹窗
 * @author Created by 天晴 on 2024/7/23 15:11
 * @since 1.0.0
 **/
class TogetherReceiveDialog(
    context: Context,
    val data: UserConsumableDailyRewardResp,
) : CenterPopupView(context) {


    private lateinit var mBinding: DialogTogetherReceiveBinding

    private val mAdapter by lazy {
        TogetherReceiveAdapter()
    }
    private val mAdapter2 by lazy {
        TogetherDailyAdapter()
    }

    override fun getImplLayoutId() = R.layout.dialog_together_receive

    override fun onCreate() {
        super.onCreate()
        mBinding = DialogTogetherReceiveBinding.bind(popupImplView)
        if (data.list.size > 1) {
            mBinding.recycler.apply {
                adapter = mAdapter
                layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
            }
            mAdapter.setList(data.rewards)
        } else {
            mBinding.recycler.visibility = GONE
        }

        if (data.list.isNotEmpty()) {
            if (data.list.size == 1) {
                mBinding.leftImg.visibility = INVISIBLE
                mBinding.rightImg.visibility = INVISIBLE
            }

            mBinding.leftImg.setOnClickListener {
                val upDataIndex = mBinding.viewpager.currentItem - 1
                if (mAdapter2.getItemOrNull(upDataIndex) != null) {
                    mBinding.viewpager.setCurrentItem(upDataIndex, true)
                }
            }
            mBinding.rightImg.setOnClickListener {
                val nextDataIndex = mBinding.viewpager.currentItem + 1
                if (mAdapter2.getItemOrNull(nextDataIndex) != null) {
                    mBinding.viewpager.setCurrentItem(nextDataIndex, true)
                }
            }
            mBinding.viewpager.adapter = mAdapter2
            mAdapter2.setList(data.list)
            mBinding.leftImg.visibility = INVISIBLE

            mBinding.viewpager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                override fun onPageSelected(position: Int) {
                    super.onPageSelected(position)
                    if (data.list.size > 1) {
                        if (position == 0) {
                            mBinding.leftImg.visibility = INVISIBLE
                            mBinding.rightImg.visibility = VISIBLE
                        } else if (position == data.list.size - 1) {
                            mBinding.rightImg.visibility = INVISIBLE
                            mBinding.leftImg.visibility = VISIBLE
                        } else {
                            mBinding.leftImg.visibility = VISIBLE
                            mBinding.rightImg.visibility = VISIBLE
                        }
                    }

                }
            })
        }
        mBinding.ok.setOnClickListener {
            dismiss()
        }

        if (AppCodeUtils.isAR()) {
            mBinding.leftImg.scaleX = -1f
            mBinding.rightImg.scaleX = -1f
        }
    }


    companion object {

        fun show(context: Context, data: UserConsumableDailyRewardResp) {
            XPopup.Builder(context)
                .moveUpToKeyboard(false)
                .isDestroyOnDismiss(true)
                .asCustom(TogetherReceiveDialog(context, data))
                .show()
        }
    }

}